<?php

namespace App\Admin\Controllers;

use App\Models\Test;
use App\Models\SheTai;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Widgets\Form as EForm;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use DB;
use Request;
use Illuminate\Support\Facades\Log;
use App\Models\AdminRoleUsers;
use App\Models\AdminUserPermissions;


use Encore\Admin\Auth\Database\Administrator;

require_once('common.php');

class testController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '测试';

    function getreport(){

      $rid=Request::input('rid');
      //$oldData = Test::findOrFail($rid)->value('html');
      $report = Test::where("id",$rid)->get()->first();
      return $report['html'];
      /*
      $data = json_decode($oldData,true);      
    //$meaning = SheTai::where("name","红舌")->get();
           

      
      
      for($i = 0;$i < count($data['char']);$i++){
        $a = $data['char'][$i]['type'];
        $result = SheTai::where("name",$a)->get()->first();
        $sym = $data['char'][$i]['probability'];
        (float)$sym <= 0.4 ? $data['char'][$i]['sym'] = "-" : $data['char'][$i]['sym'] = "+"; 
        if($result){
          $b = $result['character'];
          $c = $result['meaning'];
          $data['char'][$i]['text'] = "【".$a."】".$b.$c;
          $data['char'][$i]['class'] = $result['type'];
        } else {
          $data['char'][$i]['text'] = "未找到";
          $data['char'][$i]['class'] = "未找到";
        }      
      }
      return View('test',['data'=>$data]);
      
      */
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Test());
        $grid->disableExport();
        //$grid->disableCreateButton();
        $grid->disableColumnSelector();
        $grid->disableFilter();
        $grid->disableRowSelector();


        //$grid->column('id', __('测试号'));
        $grid->column('name', __('测试姓名'));
        $grid->column('report',__('检测'))->display(function($report){
            $data = json_decode($report,true);
            return $data ? $data['message'] : '未检测';
        });
        $grid->column('id', __('测试报告'))->display(function($id){
          //$data = Test::findOrFail($id)->value('id');
            $href='/admin/api/get_reports?rid='.$id;
            return "<a href='$href' target='_blank'>查看报告</a>";
        });
        $grid->column('img',__('测试图片'))->display(function($img){
          return $img ? $img : '未上传图片';
        });
//        $grid->column('stat', __('是否在职'))->bool();
        

        return $grid;
    }
    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
      /*
        $show = new Show(Test::findOrFail($id));

        $show->field('id', __('测试号'));
        $show->field('name', __('测试名'));
        $show->field('img',__('测试图片'));
        $show->field('report', __('测试报告'));

        return $show;
        */
        
        $data = Test::findOrFail($id)->value('report');
        $data = json_decode($data, true);
        return View('test', ['data'=>$data]);
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Test());
        /*
        $form->tools(function (Form\Tools $tools) {
                $tools->disableDelete();
                });
                */


        $form->text('name', __('测试姓名'));

        $form->text('report', __('测试报告'));
        $form->hidden('id');
        $form->image('img', __('报告图片'))->uniqueName();
        
          

         $form->saved(function (Form $form) {
             if (substr($form->model()->img,0,4) != 'http') {
             $imgpath = 'https://www.bjbayes.com:9501/upload/'. $form->model()->img;
             $form->model()->img=$imgpath;
             $report_data = file_get_contents('http://www.aitongue.cn:3010/9tiAnalysis?imgpath='.$imgpath);
             
             // 组合报告数据结构，保存报告
             $data = json_decode($report_data,true);
             $form->model()->html = "报告";
             
             if($data['message'] == "调用成功") {
                 $form->model()->html = "报告1";
                 for($i = 0;$i < count($data['char']);$i++){
                  $a = $data['char'][$i]['type'];
                  $result = SheTai::where("name",$a)->get()->first();
                  $sym = $data['char'][$i]['probability'];
                  (float)$sym <= 0.4 ? $data['char'][$i]['sym'] = "-" : $data['char'][$i]['sym'] = "+"; 
                  if($result){
                    $b = $result['character'];
                    $c = $result['meaning'];
                    $data['char'][$i]['text'] = "【".$a."】".$b.$c;
                    $data['char'][$i]['class'] = $result['type'];
                  } else {
                    $data['char'][$i]['text'] = "未找到";
                    $data['char'][$i]['class'] = "未找到";
                  }      
                }
                $form->model()->html = View('test',['data'=>$data]);
             }
                                                
             $form->model()->report=$report_data;
             $form->model()->save();                          
             }
             
});
        


        $form->footer(function ($footer) {
                $footer->disableReset();
                //$footer->disableSubmit();
                $footer->disableViewCheck();
                $footer->disableEditingCheck();
                $footer->disableCreatingCheck();

                });

        return $form;
    }
}